
from google.appengine.ext import db

from Model import Blog, Category

import string
from datetime import datetime
import logging

class BlogDao:
    
    def add(self, title, content, category_name, tags):

        if(title == '' or content == ''):
            return False
        else:
            category = self.getCategoryOfName(category_name)
            tag_list = string.split(tags)
        
            blog = Blog(title=title, content=content)
            if(category):
                blog.category = category
            blog.tag_list = tag_list

            blog.create_date = datetime.today()
            blog.modify_date = datetime.today()
            blog.display = True

            db.put(blog)

            return True


    def setDisplay(self, key, display = True):

        blog = Blog.get(key)
        if(blog):
            blog.modify_date = datetime.today()
            blog.display = display

            db.put(blog)

            return True
        else:
            return False


    def set(self, key, title, content, category_name, tags):

        if(title == '' or content == ''):
            return False
        else:

            blog = Blog.get(key)
            if(blog):
                blog.title = title
                blog.content = content

                category = self.getCategoryOfName(category_name)
                if(category):
                    blog.category = category

                blog.tag_list = string.split(tags)
                blog.modify_date = datetime.today()

                db.put(blog)

                return True
            else:
                return False

    def delete(self, key):
        blog = Blog.get(key)
        if(blog):
            db.delete(blog) 
            return True
        else:
            return False

    def getPrev(self, cur_blog, limit = 1):
        if cur_blog :
            query = Blog.all()
            query.filter('create_date >', cur_blog.create_date)
            query.order('create_date')
            blogs = query.fetch(limit)
            blogs.reverse()
            return blogs
        else:
            return []


    def getNext(self, cur_blog, limit = 1):
        if cur_blog :
            query = Blog.all()
            query.filter('create_date <', cur_blog.create_date)
            query.order('-create_date')
            return query.fetch(limit)
        else:
            return []

    def getList(self, upto_datetime=None, category_name = '', yeardate='', show_hide=False, itemPerPage=10):

        query = Blog.all()

        if upto_datetime:
            query.filter('create_date <=', upto_datetime)

        if(category_name != ''):
            category = self.getCategoryOfName(category_name)
            if(category):
                query.filter('category =', category)

        if(yeardate != ''):
            try:
                yd = datetime.strptime(yeardate, '%Y-%m')
                yd2 = yd
                yd2.month = ( yd2.month + 1 ) % 12
                query.filter('create_date >=', yd)
                query.filter('create_date <', yd2)
            except ValueError:
                pass

        if show_hide != True:
            query.filter('display', True)

        query.order('-create_date')

        blogList = query.fetch(itemPerPage + 1)

        return blogList

    def get(self, key):
        return Blog.get(key)         

    def setCategory(self, key, categoryName):
        blog = Blog.get(key)
        if(blog):
            category = self.getCategoryOfName(category_name)
            if(category):
                blog.category = category
                db.put(blog)
                return True
            else:
                return False
        else:
            return False

    def setTag(self, key, tagList):

        return True

    def delEntry(self, key):

        return True

    def setEntry(self, key, title, content):

        return True






    def getCategoryList(self):
        query = db.GqlQuery("SELECT * FROM Category")
        categoryList = query.fetch(100)
        return categoryList

    def getCategoryOfName(self, name):
        query = db.GqlQuery("SELECT * FROM Category WHERE name = :1", name)
        category = query.get()
        return category

    def addCategory(self, name):
        query = db.GqlQuery("SELECT * FROM Category WHERE name = :1", name)
        category = query.get()
        if category:
            return False
        else:
            category = Category(name=name)
            db.put(category)
            return True
    
    def setCategory(self, key, new_name):
        category = Category.get(key) 
        if category:
            category.name = new_name
            db.put(category)
            return True
        else:
            return False

    def deleteCategory(self, key):
        """
        check no blog under this category, then delete this 
        """
        category = Category.get(key)
        if category :
            query = db.GqlQuery("SELECT * FROM Blog WHERE category = :1", category)
            blogInThisCategory = query.get()
            if blogInThisCategory :
                return False
            else:
                db.delete(category) 
                return True
        else:
            return False


